package com.coocaa.typhoon.api.third.volcengine.service;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.coocaa.cirrus.common.base.util.RetryUtil;
import com.coocaa.cirrus.common.third.filestore.config.TosProperties;
import com.coocaa.typhoon.api.third.volcengine.yml.VolcConfigYml;
import com.volcengine.model.imagex.v2.GetImageOCRV2Body;
import com.volcengine.model.imagex.v2.GetImageOCRV2Query;
import com.volcengine.model.imagex.v2.GetImageOCRV2Res;
import com.volcengine.model.imagex.v2.GetImageOCRV2ResResultGeneralResultItem;
import com.volcengine.service.imagex.v2.ImagexService;
import com.volcengine.service.vikingDB.common.DataObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2025/10/13 10:12
 * @description
 */
@Slf4j
@Service
public class ImageOCRService {
    @Autowired
    private ImagexService imagexService;
    @Autowired
    private VolcConfigYml  volcConfigYml;

    public String ocr(String url) {
        Assert.hasText(url, "url不能为空");
        GetImageOCRV2Query query = new GetImageOCRV2Query();
        query.setServiceId(volcConfigYml.getOcrServiceId());
        GetImageOCRV2Body body = new GetImageOCRV2Body();
        body.setScene("general");
        body.setImageUrl(url);
        GetImageOCRV2Res res = doOCR(query, body);
        if (res == null || null == res.getResult()) {
            return "";
        }
        List<GetImageOCRV2ResResultGeneralResultItem> generalResult = res.getResult().getGeneralResult();
        if (CollectionUtils.isEmpty(generalResult)) {
            return "";
        }
        StringBuffer info = new StringBuffer();
        for (GetImageOCRV2ResResultGeneralResultItem item : generalResult) {
            info.append(item.getContent())
                    .append(" ");
        }
        if (info.length() > 0) {
            info.setLength(info.length() - 1);
        }
        return info.toString();
    }


    private GetImageOCRV2Res doOCR(GetImageOCRV2Query query, GetImageOCRV2Body body) {
        String bodyStr = JSON.toJSONString(body);
        final String key = SecureUtil.md5(bodyStr);
        log.info("doOCR: {}", bodyStr);
        long start = System.currentTimeMillis();
        try {
            GetImageOCRV2Res resp = RetryUtil.callWithThrow(key, () -> doOCRThrowException(query, body), 3, 3000);
            long end = System.currentTimeMillis();
            log.info("doOCR cost: {}秒", (end - start) / 1000.0f);
            return resp;
        } catch (Exception e) {
            log.error("doOCR", e);
            return null;
        }
    }

    private GetImageOCRV2Res doOCRThrowException(GetImageOCRV2Query query, GetImageOCRV2Body body) {
        try {
            return imagexService.getImageOCRV2(query, body);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
